﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HDFSViewer.Utility
{
    public static class ByteUtility
    {
        public static MemoryStream GetBytesFromHexString(string hex)
        {
            MemoryStream ms = new MemoryStream();
            try
            {
                for (int i = 0; i < hex.Length; )
                {
                    if (i + 2 == hex.Length || hex[i + 2] == '-')
                    {
                        ms.WriteByte(byte.Parse(hex.Substring(i, 2), System.Globalization.NumberStyles.HexNumber));
                        if (i + 2 == hex.Length)
                        {
                            i += 2;
                        }
                        else i += 3;
                    }
                    else
                    {
                        ms = null;
                        break;
                    }
                }
            }
            catch
            {
                ms = null;
            }
            return ms;
        }

        public static string GetHexStringFromBytes(byte[] bytes)
        {
            return BitConverter.ToString(bytes);
        }
    }
}
